# **********************************************************************
# *
# * PostGIS - Spatial Types for PostgreSQL
# * http://postgis.net
# *
# * Copyright 2022 Sandro Santilli <strk@kbt.io>
# * Copyright 2008 Paul Ramsey, Mark Cave-Ayland
# *
# * This is free software; you can redistribute and/or modify it under
# * the terms of the GNU General Public Licence. See the COPYING file.
# *
# **********************************************************************

top_builddir = @top_builddir@
srcdir = @srcdir@
top_srcdir = @top_srcdir@

CC=@CC@
CFLAGS=@CFLAGS@
SHELL = @SHELL@
LIBTOOL = @LIBTOOL@

CUNIT_LDFLAGS=@CUNIT_LDFLAGS@ @GEOS_LDFLAGS@
CUNIT_CPPFLAGS = \
	-I$(srcdir)/.. \
	-I$(top_builddir)/liblwgeom \
	-I$(top_srcdir)/liblwgeom \
	-I.. \
	@CUNIT_CPPFLAGS@ \
	@CPPFLAGS@

# GTK includes and libraries
GTK_CFLAGS = @GTK_CFLAGS@ @IGE_MAC_CFLAGS@
GTK_LIBS = @GTK_LIBS@ @IGE_MAC_LIBS@
GTK_WIN32_FLAGS = @GTK_WIN32_FLAGS@

# PostgreSQL frontend CPPFLAGS and LDFLAGS (for compiling and linking with libpq)
PGSQL_FE_CPPFLAGS=@PGSQL_FE_CPPFLAGS@
PGSQL_FE_LDFLAGS=@PGSQL_FE_LDFLAGS@

# liblwgeom
LIBLWGEOM = $(top_builddir)/liblwgeom/.libs/liblwgeom.la

# iconv flags
ICONV_LDFLAGS=@ICONV_LDFLAGS@
ICONV_CFLAGS=@ICONV_CFLAGS@

# GetText includes and libraries
GETTEXT_CFLAGS = @GETTEXT_CFLAGS@
GETTEXT_LDFLAGS = @GETTEXT_LDFLAGS@ @LIBINTL@

# Proj flags
PROJ_CFLAGS=@PROJ_CPPFLAGS@
PROJ_LDFLAGS=@PROJ_LDFLAGS@

# Built out CFLAGS with ICONV and GETTEXT
CFLAGS += $(GETTEXT_CFLAGS) $(ICONV_CFLAGS) $(PROJ_CFLAGS)

# Build full linking line
LDFLAGS = $(GEOS_LDFLAGS) $(GETTEXT_LDFLAGS) $(PGSQL_FE_LDFLAGS) $(ICONV_LDFLAGS) $(CUNIT_LDFLAGS) $(PROJ_LDFLAGS)

VPATH = $(srcdir)

# Object files
OBJS=	\
	cu_shp2pgsql.o \
	cu_tester.o

ifneq (@PG_CONFIG@,no)
OBJS +=	\
	cu_pgsql2shp.o
endif

LOADER_OBJS= \
	../dbfopen.o \
	../shpopen.o \
	../getopt.o \
	../shpcommon.o \
	../safileio.o \
	../shp2pgsql-core.o

ifneq (@PG_CONFIG@,no)
LOADER_OBJS +=	\
	../pgsql2shp-core.o
endif

# If we couldn't find the cunit library then display a helpful message
ifeq ($(CUNIT_LDFLAGS),)
all: requirements_not_met_cunit
check: requirements_not_met_cunit
else

$(LOADER_OBJS):
	$(MAKE) -C ..

# Build the unit tester
all: cu_tester

# Build and run the unit tester
check: cu_tester
	@./cu_tester

endif

# Build the main unit test executable
cu_tester: $(LOADER_OBJS) $(OBJS)
	$(LIBTOOL) --mode=link \
	$(CC) $(CFLAGS) $^ -o $@ $(LIBLWGEOM) $(LDFLAGS)

# Command to build each of the .o files
$(OBJS): %.o: %.c
	$(CC) $(CFLAGS) $(CUNIT_CPPFLAGS) $(GTK_CFLAGS) $(PGSQL_FE_CPPFLAGS) -c -o $@ $<

# Clean target
clean:
	rm -f $(OBJS)
	rm -f cu_tester

distclean: clean
	rm -f Makefile

# Requirements message
requirements_not_met_cunit:
	@echo
	@echo "WARNING:"
	@echo
	@echo "configure was unable to find CUnit which is required for unit testing."
	@echo "In order to enable unit testing, you must install CUnit and then re-run configure."
	@echo
